﻿using System;
using System.Collections.Generic;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary
{
    public class CustomerModule
    {
        private readonly CustomerDAO _custDAO = new CustomerDAO();

        public List<Customer> GetCustomer()
        {
            return _custDAO.SelectCustomer();
        }

        public bool CreateCustomer(string pIdentityNumber, string pCustomerName, string pCustomerPhone,
                                   string pCustomerEmail, bool pCustomerSex, out string account)
        {
            string err;
            var cust = new Customer
                           {
                               CustomerName = pCustomerName,
                               CustomerEmail = pCustomerEmail,
                               CustomerSex = pCustomerSex,
                               CustomerAccount = Utility.CreateAccount(pCustomerName)
                           };
            if (pIdentityNumber.Trim() != "")
                cust.CustomerIdentifyNo = pIdentityNumber;

            if (pCustomerPhone.Trim() != "")
                cust.CustomerPhone = pCustomerPhone;

            cust.ModDay = DateTime.Today;

            if (ValidData(pCustomerName, pCustomerPhone, pCustomerEmail, out err))
            {
                return _custDAO.InsertCustomer(cust, out account);
            }
            throw new Exception(err);
        }

        public bool CheckCustomerEmail(string email)
        {
            return _custDAO.CheckCustomerEmail(email);
        }

        public bool CheckCustomerAccount(string account)
        {
            return _custDAO.CheckCustomerAccount(account);
        }


        private bool ValidData(string pCustomerName, string pCustomerPhone,
                               string pCustomerEmail, out string err)
        {
            if (pCustomerName.Trim() == "")
            {
                err = "Null data";
                return false;
            }

            if (!Utility.validPhoneEmail(pCustomerPhone, pCustomerEmail, out err))            
                return false;            
            err = "";
            return true;
        }


        public bool ModifyCustomer(int pId, string pIdentityNumber, string pCustomerName, string pCustomerPhone,
                                   string pCustomerEmail, bool pCustomerSex)
        {
            string err;

            var cust = new Customer
                           {
                               CustomerID = pId,
                               CustomerName = pCustomerName.Trim(),
                               CustomerEmail = pCustomerEmail,
                               CustomerSex = pCustomerSex
                           };
            if (pIdentityNumber.Trim() != "")
                cust.CustomerIdentifyNo = pIdentityNumber;

            if (pCustomerPhone.Trim() != "")
                cust.CustomerPhone = pCustomerPhone;

            cust.ModDay = DateTime.Now;
            if (ValidData(pCustomerName, pCustomerPhone, pCustomerEmail, out err))
            {
                return _custDAO.UpdateCustomer(cust);
            }
            throw new Exception(err);
        }

        public List<Customer> GetCustomers(string name, string email, string acount)
        {
            return _custDAO.SelectCustomers(name, email, acount);
        }

        public Customer GetCustomer(int customerId)
        {
            return _custDAO.SelectCustomer(customerId);
        }

        public Customer GetCustomerByEmail(string email)
        {
            return _custDAO.SelectCustomerByEmail(email);
        }

        public Customer GetCustomerByAccount(string acount)
        {
            return _custDAO.SelectCustomerByAccount(acount);
        }
    }
}